package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.config.StorageService;
import com.ruoyi.framework.util.ResponseUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.SysStorage;
import com.ruoyi.system.service.IStorageService;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.base.AjaxResult;

/**
 * 文件存储 信息操作处理
 * 
 * @author lzy
 * @date 2019-03-18
 */
@Controller
@RequestMapping("/system/storage")
public class StorageController extends BaseController {
	private String prefix = "system/storage";

	@Autowired
	private IStorageService storageService;
	@Autowired
	private StorageService uploadService;

	@RequiresPermissions("system:storage:view")
	@GetMapping()
	public String storage() {
		return prefix + "/storage";
	}

	@PostMapping("/upload")
	@ResponseBody
	@Transactional
	public AjaxResult upload(@RequestParam("file") MultipartFile file) throws IOException {

	try {
			String originalFilename = file.getOriginalFilename();
			String url = uploadService.store(file.getInputStream(), file.getSize(), file.getContentType(),
					originalFilename);

			Map<String, Object> data = new HashMap<>();
			data.put("url", url);

			AjaxResult ajax = AjaxResult.success();
			ajax.put("fileName", originalFilename);
			ajax.put("url", url);
			return ajax;
		} catch (Exception e) {
			return AjaxResult.error(e.getMessage());
		}

	}

	/**
	 * 查询文件存储列表
	 */
	@RequiresPermissions("system:storage:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysStorage storage) {
		startPage();
		List<SysStorage> list = storageService.selectStorageList(storage);
		return getDataTable(list);
	}

	/**
	 * 导出文件存储列表
	 */
	@RequiresPermissions("system:storage:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(SysStorage storage) {
		List<SysStorage> list = storageService.selectStorageList(storage);
		ExcelUtil<SysStorage> util = new ExcelUtil<SysStorage>(SysStorage.class);
		return util.exportExcel(list, "storage");
	}

	/**
	 * 新增文件存储
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存文件存储
	 */
	@RequiresPermissions("system:storage:add")
	@Log(title = "文件存储", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(SysStorage storage) {
		return toAjax(storageService.insertStorage(storage));
	}

	/**
	 * 修改文件存储
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
		SysStorage storage = storageService.selectStorageById(id);
		mmap.put("storage", storage);
		return prefix + "/edit";
	}

	/**
	 * 修改保存文件存储
	 */
	@RequiresPermissions("system:storage:edit")
	@Log(title = "文件存储", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(SysStorage storage) {
		return toAjax(storageService.updateStorage(storage));
	}

	/**
	 * 删除文件存储
	 */
	@RequiresPermissions("system:storage:remove")
	@Log(title = "文件存储", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(storageService.deleteStorageByIds(ids));
	}

}
